package com.zyy.array;
import java.util.Arrays;
import java.util.Scanner;
public class StudentsInquireScore {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入五个课程名：");
        String course[] = new String[5];
        for(int i = 0; i < 5; i++){
            course[i] = scan.next();
        }
        System.out.println("请输入六个学生姓名");
        String name[] = new String[6];
        for(int i = 0; i < 6; i++){
            name[i] = scan.next();
        }
        int score[][] = new int[6][5];

        System.out.println("请输入对应的分数：");
        for(int i = 0; i < 6; i++){
            for(int j = 0; j < 5; j++){
                score[i][j] = scan.nextInt();
            }
        }

        for(int i = 0; i < 5; i++){

            System.out.print(String.format("%10s",course[i]));
        }
        System.out.print("\n");
        for(int i = 0; i < 6; i++){
            System.out.print(String.format("%-9s",name[i]));
                for(int j = 0; j < 5; j++){
                    System.out.print(String.format("%-9s",score[i][j]));
                }
            System.out.print("\n");
        }

        while(true){
            System.out.print("请输入命令：");
            String order = scan.next();

            if(order.equals("avg")){
                String find = scan.next();
                boolean jdN = false;
                boolean jdC = false;
                boolean jude = false;
                for(int i = 0; i < name.length; i++){
                    if(name[i].indexOf(find) != -1) {
                        double avg = 0;
                        jdN = true;
                        for (int j = 0; j < course.length; j++) {
                            avg += score[i][j];
                        }
                        avg /= course.length;
                        System.out.println("" + find + "的平均分是：" + String.format("%.2f",avg));
                        jude = true;
                        break;
                    }
                }
                if(jude == false){
                    for(int i = 0; i < course.length; i++) {
                        if (course[i].indexOf(find) != -1) {
                            jdC = true;
                            double avg = 0;
                            for (int j = 0; j < name.length; j++) {
                                avg += score[j][i];
                            }
                            avg /= name.length;
                            System.out.println("" + find + "的平均分是：" + String.format("%.2f",avg));
                            break;
                        }
                    }
                }
                if(jdN == false && jdC == false){
                    System.out.println("你输入的既不是课程名，也不是学生名");
                }
            }
            if(order.equals("get")){
                String Name = scan.next();
                String Course = scan.next();

                boolean jdN = false;
                boolean jdC = false;
                for(int i = 0; i < name.length; i++) {
                    if (name[i].indexOf(Name) != -1) {
                        jdN = true;
                        for (int j = 0; j < course.length; j++) {
                            if(course[j].indexOf(Course) != -1){
                                System.out.print("" + Name + "的" + Course +"成绩是" + score[i][j]);
                                jdC = true;
                                break;
                            }
                        }
                        break;
                    }

                }
                for (int j = 0; j < course.length; j++) {
                    if(course[j].indexOf(Course) != -1){
                        jdC = true;
                        break;
                    }
                }
                if(jdN == false){
                    System.out.print("没有" + Name + "这个人");
                }
                if(jdC == false){
                    System.out.print("没有" + Course + "这个课程");
                }
                System.out.print("\n");
            }
            if(order.equals("sort")){
                String find = scan.next();
                boolean jdC = false;
                for(int i = 0; i < course.length; i++) {
                    if (course[i].indexOf(find) != -1) {
                        jdC = true;
                        int arr[] = new int[6];
                        String xm[]= new String[101];
                        for(int j = 0; j < name.length; j++){
                            arr[j] = score[j][i];
                            xm[score[j][i]] = name[j];
                        }
                        Arrays.sort(arr);
                        System.out.println("名次\t\t姓名\t\t" + find);
                        for (int j = 0; j < arr.length; j++) {
                            System.out.print(String.format("%-9d",j + 1));
                            System.out.print(String.format("%-9s",xm[arr[j]]));
                            System.out.println(String.format("%-9s",arr[j]));
                        }
                        break;
                    }

                }
                if(jdC == false){
                    System.out.println("没有这门课程");
                }
            }
            if(order.equals("exit")){
                System.out.println("退出查询系统！byebye！");
                break;
            }
        }
    }
}
